package com.ruoyi.web.controller.vqms;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.VQMSConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.QRCodeUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.vqms.domain.VQMSFactory;
import com.ruoyi.vqms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工厂 控制器
 */
@RestController
@RequestMapping("/vqms/factory")
public class VQMSFactoryController extends BaseController {
    @Autowired
    private IVQMSFactoryService factoryService;
    @Autowired
    private IVQMSStorehouseService storehouseService;
    @Autowired
    private IVQMSStorehouseLocationService storehouseLocationService;
    @Autowired
    private IVQMSQueueAreaService queueAreaService;
    @Autowired
    private IVQMSVisitRecordYuchaiService visitRecordYuchaiService;
    @Autowired
    private IVQMSVisitRecordYuchiService visitRecordYuchiService;
    @Autowired
    private IVQMSQueueReservationService queueReservationService;
    @Autowired
    private QRCodeUtils qrCodeUtils;

    /**
     * 查询工厂列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('vqms:factory:list')")
    @RequestMapping("/getList")
    public AjaxResult getList(VQMSFactory vqmsFactory){
        return AjaxResult.success(factoryService.selectList(vqmsFactory));
    }

    /**
     * 查询工厂列表
     */
    @PreAuthorize("@ss.hasPermi('vqms:factory:list')")
    @RequestMapping("/list")
    public TableDataInfo list(VQMSFactory vqmsFactory){
        startPage();
        List<VQMSFactory> list=factoryService.selectList(vqmsFactory);
        return getDataTable(list);
    }

    @Value("${vqms.url}")
    private String vqmsUrl;//二维码访问url

    /**
     * 新增工厂
     */
    @Log(title = "新增工厂", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('vqms:factory:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VQMSFactory vqmsFactory){
        vqmsFactory.setCreateBy(SecurityUtils.getUsername());
        //生成二维码
        if(vqmsFactory.getIsQueue() == VQMSConstants.QUEUE_NO){
            String fileName= qrCodeUtils.createQRCode(VQMSConstants.QRCODE_URL_IN + vqmsFactory.getId(),vqmsFactory.getName()+"(进场)");
            vqmsFactory.setQrcodeUploadPathIn(fileName);
            vqmsFactory.setQrcodeUrlIn(vqmsUrl+fileName);
            fileName= qrCodeUtils.createQRCode(VQMSConstants.QRCODE_URL_OUT + vqmsFactory.getId(),vqmsFactory.getName()+"(出场)");
            vqmsFactory.setQrcodeUploadPathOut(fileName);
            vqmsFactory.setQrcodeUrlOut(vqmsUrl+fileName);
        }
        factoryService.add(vqmsFactory);
        return AjaxResult.success();
    }

    /**
     * 查询工厂
     */
    @PreAuthorize("@ss.hasPermi('vqms:factory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(factoryService.getById(id));
    }

    /**
     * 修改工厂
     */
    @Log(title = "修改工厂", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('vqms:factory:update')")
    @PutMapping(value = "/update")
    public AjaxResult update(@RequestBody VQMSFactory vqmsFactory)
    {
        vqmsFactory.setUpdateBy(SecurityUtils.getUsername());
        //生成二维码
        if(vqmsFactory.getIsQueue() == VQMSConstants.QUEUE_NO && StringUtils.isEmpty(vqmsFactory.getQrcodeUploadPathIn())){
            String fileName= qrCodeUtils.createQRCode(VQMSConstants.QRCODE_URL_IN + vqmsFactory.getId(),vqmsFactory.getName()+"(进场)");
            vqmsFactory.setQrcodeUploadPathIn(fileName);
            vqmsFactory.setQrcodeUrlIn(vqmsUrl+fileName);
            fileName= qrCodeUtils.createQRCode(VQMSConstants.QRCODE_URL_OUT + vqmsFactory.getId(),vqmsFactory.getName()+"(出场)");
            vqmsFactory.setQrcodeUploadPathOut(fileName);
            vqmsFactory.setQrcodeUrlOut(vqmsUrl+fileName);
        }
        factoryService.update(vqmsFactory);
        return AjaxResult.success();
    }

    /**
     * 删除工厂
     */
    @Log(title = "删除工厂", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('vqms:factory:delete')")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        for(Long factoryId : ids){
            int num=storehouseService.countByFactoryId(factoryId);
            if(num>0){
                return AjaxResult.error("删除失败，请先删除该工厂的仓库！");
            }
            int num2=storehouseLocationService.countByFactoryId(factoryId);
            if(num2>0){
                return AjaxResult.error("删除失败，请先删除该工厂的仓库库位！");
            }
            int num3=queueAreaService.countByFactoryId(factoryId);
            if(num3>0){
                return AjaxResult.error("删除失败，请先删除该工厂的排队队列！");
            }
            int num4=visitRecordYuchaiService.countByFactoryId(factoryId);
            if(num4>0){
                return AjaxResult.error("删除失败，请先删除该工厂的玉柴访客记录！");
            }
            int num5=visitRecordYuchiService.countByFactoryId(factoryId);
            if(num5>0){
                return AjaxResult.error("删除失败，请先删除该工厂的玉驰访客记录！");
            }
            int num6=queueReservationService.countByFactoryId(factoryId);
            if(num6>0){
                return AjaxResult.error("删除失败，请先删除该工厂的预约记录！");
            }
        }
        factoryService.delete(ids);
        return AjaxResult.success();
    }

}
